package com.learn.project2.service.admin.ad;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.learn.project2.bean.MarketAd;
import com.learn.project2.bean.MarketAdExample;
import com.learn.project2.mapper.MarketAdMapper;
import com.learn.project2.model.bo.admin.ad.AdCreateBO;
import com.learn.project2.model.bo.admin.ad.AdDeleteBO;
import com.learn.project2.model.bo.admin.ad.AdListBO;
import com.learn.project2.model.bo.admin.ad.AdUpdateBO;
import com.learn.project2.model.vo.admin.ad.AdCreateVO;
import com.learn.project2.model.vo.admin.ad.AdListVO;
import com.learn.project2.model.vo.admin.ad.AdUpdateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * @author 岳子昂
 * @version 1.0
 * @description:
 * @date 2022-01-07 20:54
 */
@Service
public class AdServiceImpl implements AdService {
    @Autowired
    MarketAdMapper adMapper;

    /**
     * @description:
     * @param:
     * @return:
     * @author 岳子昂
     * @date: 2022-01-07 21:15
     */
    @Override
    public AdListVO list(AdListBO adListBO) {
        Integer limit = adListBO.getLimit();
        String order = adListBO.getOrder();
        Integer page = adListBO.getPage();
        String sort = adListBO.getSort();
        String name = adListBO.getName();
        String content = adListBO.getContent();

        // 开启pageHelper
        PageHelper.startPage(page, limit);
        MarketAdExample example = new MarketAdExample();
        // 执行逆向工程，获取排好序的POJO list
        example.setOrderByClause(sort + " " + order);

        MarketAdExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (name != null){
            criteria.andNameLike("%"+name+"%");
        }
        if (content != null){
            criteria.andContentLike("%"+content+"%");
        }
        List<MarketAd> marketAds = adMapper.selectByExample(example);

        // 获取传回值的voList
        List<AdListVO.ListBean> list = new ArrayList<>();

        for (MarketAd item : marketAds) {
            AdListVO.ListBean listBean = new AdListVO.ListBean();
            listBean.setId(item.getId());
            listBean.setName(item.getName());
            listBean.setLink(item.getLink());
            listBean.setUrl(item.getUrl());
            listBean.setPosition(((int) item.getPosition()));
            listBean.setContent(item.getContent());
            listBean.setEnabled(item.getEnabled());
            listBean.setAddTime(item.getAddTime().toString());
            listBean.setUpdateTime(item.getUpdateTime().toString());
            listBean.setDeleted(item.getDeleted());
            boolean ifDelete = listBean.getDeleted();

            // 将赋值的值放入最终要传走的list中，但将delete是true的剔除
            if (ifDelete == false){
                list.add(listBean);
            }
        }

        PageInfo<MarketAd> pageInfo = new PageInfo<>(marketAds);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();

        // 最终传走的对象
        AdListVO adListVO = new AdListVO();
        adListVO.setTotal(((int) total));
        adListVO.setPages(pages);
        adListVO.setLimit(limit);
        adListVO.setPage(page);
        adListVO.setList(list);

        return adListVO;
    }

    /**
     * @description:
     * @param:
     * @return:
     * @author 岳子昂
     * @date: 2022-01-07 23:43
     */
    @Override
    @Transactional
    public AdCreateVO create(AdCreateBO adCreateBO) {
        String name = adCreateBO.getName();
        String content = adCreateBO.getContent();
        String url = adCreateBO.getUrl();
        String link = adCreateBO.getLink();
        int position = adCreateBO.getPosition();
        Boolean enabled = adCreateBO.getEnabled();

        // 生成POJO对象
        MarketAd m1 = new MarketAd();

        m1.setName(name);
        m1.setContent(content);
        m1.setUrl(url);
        m1.setLink(link);
        m1.setPosition(((byte) position));
        m1.setEnabled(enabled);
        m1.setAddTime(new Date());
        m1.setUpdateTime(new Date());
        m1.setDeleted(false);
        // 判断link是否为空
        if (link != null) {
            adMapper.insert(m1);
        }

        Integer id = m1.getId();
        AdCreateVO adCreateVO = new AdCreateVO();

        adCreateVO.setId(id);
        adCreateVO.setName(name);
        adCreateVO.setLink(link);
        adCreateVO.setUrl(url);
        adCreateVO.setPosition(position);
        adCreateVO.setContent(content);
        adCreateVO.setEnabled(enabled);
        adCreateVO.setAddTime(m1.getAddTime().toString());
        adCreateVO.setUpdateTime(m1.getUpdateTime().toString());

        return adCreateVO;
    }

    /**
     * @description:
     * @param:
     * @return:
     * @author 岳子昂
     * @date: 2022-01-07 23:43
     */
    @Override
    @Transactional
    public AdUpdateVO update(AdUpdateBO adUpdateBO) {
        MarketAd marketAd = new MarketAd();

        Integer id = adUpdateBO.getId();
        String name = adUpdateBO.getName();
        String link = adUpdateBO.getLink();
        String url = adUpdateBO.getUrl();
        int position = adUpdateBO.getPosition();
        String content = adUpdateBO.getContent();
        Boolean enabled = adUpdateBO.getEnabled();
        String addTime = adUpdateBO.getAddTime();
        String updateTime = adUpdateBO.getUpdateTime();
        Boolean deleted = adUpdateBO.getDeleted();

        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);

        marketAd.setId(id);
        marketAd.setName(name);
        marketAd.setLink(link);
        marketAd.setUrl(url);
        marketAd.setPosition(((byte) position));
        marketAd.setContent(content);
        marketAd.setEnabled(enabled);
        try {
            marketAd.setAddTime(sdf.parse(addTime));
            marketAd.setUpdateTime(sdf.parse(updateTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        marketAd.setDeleted(deleted);

        adMapper.updateByPrimaryKeySelective(marketAd);
        AdUpdateVO adUpdateVO = new AdUpdateVO();
        adUpdateVO.setId(id);
        adUpdateVO.setName(name);
        adUpdateVO.setLink(link);
        adUpdateVO.setUrl(url);
        adUpdateVO.setPosition(position);
        adUpdateVO.setContent(content);
        adUpdateVO.setEnabled(enabled);
        adUpdateVO.setAddTime(addTime);
        adUpdateVO.setUpdateTime(new Date().toString());
        adUpdateVO.setDeleted(deleted);
        return adUpdateVO;
    }

    /**
     * @description:
     * @param:
     * @return:
     * @author 岳子昂
     * @date: 2022-01-08 00:38
     */
    @Override
    @Transactional
    public void delete(AdDeleteBO adDeleteBO) {
        Integer id = adDeleteBO.getId();
//        adMapper.deleteByPrimaryKey(id);

        // TODO:逻辑删除的代码
        MarketAd marketAd = new MarketAd();
        marketAd.setId(id);
        marketAd.setUpdateTime(new Date());
        marketAd.setDeleted(true);
        adMapper.updateByPrimaryKeySelective(marketAd);
    }
}
